package core.sceneManager
{
	import caurina.transitions.Tweener;
	
	import core.MainManager;
	import core.sceneManager.sceneSection.scene2DSection.Scene2D;
	import core.sceneManager.sceneSection.scene3DSection.Scene3D;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import loaders.Preloader;
	
	import memory.MemoryRecovery;
	import memory.MyGC;
	
	import structure.SectionList;
	
	import view.ToolBitmapData;
	
	
	public class SceneManager implements ISceneManager
	{
		private static var _insttance:SceneManager;
		private var _currentSceneId:int=-1;               //当前场景的ID
		private var _gotoSceneId:int=-1;                  //所要去的场景的ID
		private var scene2D:IScene;                      //2D场景
		private var scene3D:IScene;                      //3D场景
		private var isCreating:Boolean=false;            //是否正在创建场景
		private var preloader:Preloader;
		private var dataBase:XML;
		private var snapshot:Bitmap;                     //场景快照
		public static var sections:SectionList;
		public function SceneManager()
		{
			if(_insttance==null)
			{
				_insttance=this;
				sections=new SectionList();
			}else
			{
				trace("SceneManager不能被实例化!");
			}
		}
		public function init(xml:XML):void
		{
			dataBase=xml;
			initScript();
		}
		private function initScript():void
		{
			MainManager.getInstance().scriptManager.addApi("gotoScene",gotoScene);
		}
		/**
		 * 跳转场景
		 * 先处理预加载,预加载完毕后，就处理2D场景，最后处理3D场景
		 */ 
		public function gotoScene(id:int):void
		{
			if(id!=_currentSceneId && !isCreating && dataBase.Scene[id]!=null)
			{
				isCreating=true;
				_gotoSceneId=id;
				createSnapshot();
				removeOldScene();
				initPreloader(id);
			}
		}
		//创建一个快照，遮住场景
		private function createSnapshot():void
		{
            var data:BitmapData=ToolBitmapData.getInstance().drawDisplayObjectCustom(MainManager.getInstance().stage.stageWidth,MainManager.getInstance().stage.stageHeight,MainManager.getInstance().stage);
			snapshot=new Bitmap(data);
			MainManager.getInstance().getContainer(3).addChild(snapshot);
		}
		private function destorySnapshot():void
		{
			Tweener.addTween(snapshot,{time:3,alpha:0,onComplete:function():void{
			    if(snapshot.parent!=null)
			    {
			    	snapshot.parent.removeChild(snapshot);
			    }
			    snapshot.bitmapData.dispose();
			    snapshot=null;
			    _currentSceneId=_gotoSceneId;
			    isCreating=false;//快照删除的时候，就是场景加载完毕的时候
			}});
		}
		//处理预加载
		private function initPreloader(id:int):void
		{
			preloader=new Preloader();
			preloader.addEventListener(Event.COMPLETE,on_preload_complete);
			if(dataBase.Scene[id].Preload.length()==1)
			{
				preloader.init(XML(dataBase.Scene[id].Preload),MainManager.getInstance().getContainer(5));
				preloader.start();
			}else
			{
				preloader.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		private function on_preload_complete(e:Event):void
		{
			MemoryRecovery.getInstance().gcFun(preloader,Event.COMPLETE,on_preload_complete);
			this.scene2D=new Scene2D();
			scene2D.addEventListener(Event.COMPLETE,on_scene2D_complete);
			if(dataBase.Scene[this._gotoSceneId].Scene2D!=null && dataBase.Scene[this._gotoSceneId].Scene2D.length()>0)
			{
				scene2D.createSections(XML(dataBase.Scene[this._gotoSceneId].Scene2D));
			}else
			{
				scene2D.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		//2D场景加载完毕
		private function on_scene2D_complete(e:Event):void
		{
			MemoryRecovery.getInstance().gcFun(scene2D,Event.COMPLETE,on_scene2D_complete);
			this.scene3D=new Scene3D();
			scene3D.addEventListener(Event.COMPLETE,on_scene3D_complete);
			if(dataBase.Scene[this._gotoSceneId].Scene3D!=null && dataBase.Scene[this._gotoSceneId].Scene3D.length()>0)
			{
				scene3D.createSections(XML(dataBase.Scene[this._gotoSceneId].Scene3D));
			}else
			{
				scene3D.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		//3D场景加载完毕
		private function on_scene3D_complete(e:Event):void
		{
			MemoryRecovery.getInstance().gcFun(scene3D,Event.COMPLETE,on_scene3D_complete);
			destorySnapshot();
		}
		/**
		 * 删除旧场景
		 */ 
		private function removeOldScene():void
		{
			if(_currentSceneId!=-1)
			{
				this.scene3D["disposeEvent"]();
				sections.deleteAutoRefreshSection();
				this.scene2D.dispose();
				this.scene2D=null;
				this.scene3D.dispose();
				this.scene3D=null;
				if(this.preloader!=null) this.preloader.dispose();
				this.preloader=null;
				MyGC.gc();
			}
		}
		public static function getInstance():SceneManager
		{
			if(_insttance==null) return new SceneManager();
			return _insttance;
		}
		////////////////////////////////////////////////////////////下面的可以不用看//////////////////////////////
		public function get currentSceneId():int
		{
			return _currentSceneId;
		}
		public function get gotoSceneId():int
		{
			return _gotoSceneId;
		}
	}
}